package com.example.mobile2121606110syc0514homework;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class SingleInstanceActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvTitle;
    private Button btnStandard;
    private Button btnSingleTop;
    private Button btnSingleTask;
    private Button btnSingleInstance;
    private Button btnClose;
    private TextView tvTaskID;
    private TextView tvActivityNum;
    private RecyclerView rvActivities;
    private static int NO=0;
    public static List<String> list = new ArrayList<>();
    public static ActivitiesAdapter adapter = new ActivitiesAdapter(list);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard);
        initView();
        NO+=1;
        tvTitle.setText("SingleInstanceActivity"+NO);
        list.add(tvTitle.getText().toString());
        if (rvActivities.getAdapter() == null){
            rvActivities.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }
    @Override
    protected void onStart(){
        super.onStart();
        tvTaskID.setText("任务栈ID:"+getTaskId());
        ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.AppTask appTask=activityManager.getAppTasks().get(0);
        ActivityManager.RecentTaskInfo taskInfo=appTask.getTaskInfo();
        int num= 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            num = taskInfo.numActivities;
        }
        tvActivityNum.setText("Activity数量:"+num);
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        list.remove(tvTitle.getText().toString());
        adapter.notifyDataSetChanged();
    }
    @Override
    protected void onRestart(){
        super.onRestart();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btnStandard:
                intent.setClass(this, StandardActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleTop:
                intent.setClass(this,SingleTopActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleTask:
                intent.setClass(this, SingleTaskActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleInstance:
                intent.setClass(this, SingleInstanceActivity.class);
                startActivity(intent);
                break;
            case R.id.btnClose:
                finish();
                break;
        }
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        btnStandard = (Button) findViewById(R.id.btnStandard);
        btnSingleTop = (Button) findViewById(R.id.btnSingleTop);
        btnSingleTask = (Button) findViewById(R.id.btnSingleTask);
        btnSingleInstance = (Button) findViewById(R.id.btnSingleInstance);
        btnClose = (Button) findViewById(R.id.btnClose);
        btnStandard.setOnClickListener(this);
        btnSingleTop.setOnClickListener(this);
        btnSingleTask.setOnClickListener(this);
        btnSingleInstance.setOnClickListener(this);
        btnClose.setOnClickListener(this);
        tvTaskID = (TextView) findViewById(R.id.tvTaskID);
        tvActivityNum = (TextView) findViewById(R.id.tvActivityNum);
        rvActivities = (RecyclerView) findViewById(R.id.rvActivities);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setStackFromEnd(true);
        layout.setReverseLayout(true);
        rvActivities.setLayoutManager(layout);
    }
}